package com.lagou.task18;

import java.util.concurrent.locks.ReentrantLock;

public class AccountThreadTest extends Thread{
    private int balance;
    public static demo demo = new demo();

    public AccountThreadTest(){

    }

    public AccountThreadTest(int balance) {
        this.balance = balance;
    }

    public int getBalance() {
        return balance;
    }

    public void setBalance(int balance) {
        this.balance = balance;
    }

    @Override
    public void run() {
//        synchronized (demo) {
            getMonney(200);

//        }
    }
    //取款
    private static /*synchronized*/ void getMonney(int monney){
        //静态方法加synchronized等价与synchronized(类名.class){}
        synchronized(AccountThreadTest.class){
            int temp = 1000;
            if (temp >= monney){
                System.out.println("正在出钞，请稍后。。。");
                temp -= monney;
                try {
                    Thread.sleep(5000);
//                setBalance(temp);
//                System.out.println("剩余：" + getBalance());
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }else{
                System.out.println("余额不足！");
            }
        }
    }

    public static void main(String[] args) {
        AccountThreadTest at1 = new AccountThreadTest(1000);
        AccountThreadTest at2 = new AccountThreadTest(1000);
        at1.start();
        at2.start();
        System.out.println("主线程等待子线程结束");
        try {
            at1.join();
            System.out.println("子线程1结束");
            at2.join();
            System.out.println("子线程2结束");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("账户余额" + at1.getBalance());
        System.out.println("账户余额" + at2.getBalance());
    }
}
class demo{

}